﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using MAP;

public struct TestStruct
{
    public int X;
    public int Y;
}

/// <summary>
/// 自定义坐标类
/// </summary>
public class Position
{
    public float X;
    public float Y;
    public Position()
    {
    }
    public Position(float x, float y)
    {
        X = x;
        Y = y;
    }

}
/// <summary>
/// 用来记录建筑所有信息的数据结构
/// </summary>
public class Building
{

    public int ID;
    /// <summary>
    /// 建筑坐标
    /// </summary>
    public Position Position;
    /// <summary>
    /// 标记该建筑物是否已经被传送
    /// </summary>
    public bool HasVisited ;//= false;

    /// <summary>
    /// 数据操作类
    /// </summary>
    private DataOperation mOperation = new DataOperation();

    public float Length;
    public float Width;
    public float Height;
    public string BuildingName;
    public string BuildingTexturePath;

    public Building()
    {
    }
    public Building(Position p)
    {
        Position = p;
    }
    /// <summary>
    /// 设置访问与否的属性
    /// </summary>
    /// <param name="status"></param>
    public void setVisited(bool status)
    {
        HasVisited = status;
    }
    public void Clear()
    {
        BuildingName = null;

    }
    public bool Equals(string name)
    {
        return BuildingName == name;
    }

    /// <summary>
    /// 得到Building的详细信息
    /// </summary>
    public void getBuildingInfo()
    {
        string queryString = "select * from Building where PositionX=" + Position.X + "and PositionY=" + Position.Y;
        SqlConnection conn = mOperation.getConnnection();
        SqlCommand command = mOperation.getCommand(queryString, conn);
        SqlDataReader dr = mOperation.getDateReader(command);

        try
        {
            if (!dr.Read())
            {
                dr.Close();
                //mOperation.closeConnection(conn);
                throw new MapException("对不起，没有找到你要的数据！");
            }
            else
            {
                Length = float.Parse(dr["Length"].ToString());
                Width = float.Parse(dr["width"].ToString());
                Height = float.Parse(dr["Height"].ToString());
                BuildingName = dr["BuildingName"].ToString();
                BuildingTexturePath = dr["Texture"].ToString();
            }
        }
        catch
        {
        }
        dr.Close();
    }
}

[WebService(Namespace = "http://www.virtual3dmap.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    RECTF LastRect = new RECTF();

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public void RetrieveObject(TestStruct[] s)
    {

    }
    /// <summary>
    /// 通过输入坐标和宽度来进行查询的服务
    /// 输入起始点的左上角坐标和整个查询范围的长宽,返回一个Building的链表
    /// 返回的链表刚好大小等于所查询的大小范围
    /// 如果有建筑已经被显示过
    /// 则Building的hasVisited方法返回true;
    /// </summary>
    /// <param name="left"></param>
    /// <param name="top"></param>
    /// <param name="length"></param>
    /// <param name="height"></param>
    /// <returns></returns>
    [WebMethod]
    public Building[] GetMapData(float left, float top, float length, float height)
    {
        LastRect.left = left;
        LastRect.top = top;
        LastRect.length = length;
        LastRect.height = height;
        LastRect.CalculateVariable();
        Building[] building = new Building[1];

        List<Building> buildings = CalculateBestPosition.GetBestArea(LastRect);

        return buildings.ToArray();
        //if(buildings.Count==0)
       //     return building;
        //return buildings.ToArray();
        //if (buildingInfo == null)
        //    return false;//取出失败,没有所查询的数据
        //else
        //    return true;
       
    }
    
    [WebMethod]
    public Building[] GetMapDataFast(string locationname)
    {
        List<Building> buildingList = QueryEntrance.queryEntrance(locationname);
        return buildingList.ToArray();
    }
    [WebMethod]
    public Building[] GetMapDataOptimized(float centerX,float centerY,float width,float height, int[] existBuildingIDs)
    {
        List<Building> buildings = QueryEntrance.queryByPosition(centerX, centerY, width / 2, height / 2);
        List<Building> filteredBuildingList = new List<Building>();
        foreach (Building building in buildings)
        {
            bool found = false;
            foreach (int id in existBuildingIDs)
            {
                if (building.ID == id)
                {
                    found = true;
                    break;
                }
            }
            //Filter buildings
            if(!found)
                filteredBuildingList.Add(building);
        }
        Building[] result = { new Building() };
        if (filteredBuildingList.Count > 0)
            return filteredBuildingList.ToArray();
        return null;
    }
}

